package xyz.weizhi.rjwm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.entity.Dish;
import xyz.weizhi.rjwm.entity.Setmeal;
import xyz.weizhi.rjwm.entity.vo.DishDto;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.service.DishService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author milian
 * @Date 2021/9/25 0025 19:35
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * @return xyz.weizhi.rjwm.entity.vo.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     * @Author milian
     * @Description //获取菜品列表
     * @Date 19:51 2021/9/25 0025
     * @Param [pageNum, pageSize, name]
     **/
    @GetMapping("/page")
    public R<Page> getDishPage(@RequestParam("page") long pageNum, long pageSize, String name) {
        Page<Dish> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Dish::getName, name).orderByAsc(Dish::getSort);
        dishService.page(page, lqw);
        return R.success(page);
    }


    /**
     * @Author milian
     * @Description //新增菜品
     * @Date 19:53 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto){
        boolean b = dishService.saveDishAndDishFlavor(dishDto);
        if (b){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }

    /**
     * @Author milian
     * @Description //根据id查询详情
     * @Date 19:57 2021/9/25 0025
     * @Param [id]
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.Dish>
     **/
    @GetMapping("/{id}")
    public R<DishDto> queryDishById(@PathVariable("id") long id){
        String key = "Dish_"+id+"_1";
        redisTemplate.delete(key);
        DishDto DishDto = dishService.getDishAndDishFlavorById(id);
        return R.success(DishDto);
    }

    /**
     * @Author milian
     * @Description //修改菜品
     * @Date 19:55 2021/9/25 0025
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PutMapping
    public R<String> editDish(@RequestBody DishDto dishDto){
        boolean b = dishService.updateDishAndDishFlavor(dishDto);
        String key = "Dish_"+dishDto.getCategoryId()+"_1";
        redisTemplate.delete(key);
        if (b){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    /**
     * @Author milian
     * @Description //删除菜品
     * @Date 19:59 2021/9/25 0025
     * @Param [id]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @DeleteMapping
    public R<String> deleteDish(long ids[]){
        log.info("id:{}",ids);
        boolean b = dishService.deleteDishAndDishFlavor(ids);
        if (b){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @PostMapping("/status/{status}")
    public R<String> dishStatusByStatus(@PathVariable Integer status,long ids[]){
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        boolean b = dishService.updatDishStatusByStatus(ids,status);
        if (b){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @GetMapping("/list")
    public R<List<Dish>> queryDishList(Dish dish){
        String key = "Dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        List<Dish> list = (List<Dish>)redisTemplate.opsForValue().get(key);
        if (list!=null){
            return R.success(list);
        }
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lqw.like(dish.getName()!=null,Dish::getName,dish.getName());
        lqw.eq(Dish::getStatus,1);
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishService.list(lqw);
        redisTemplate.opsForValue().set(key,dishes,60, TimeUnit.MINUTES);
        return R.success(dishes);
    }

}

